package com.sxbbc.plat.controller.jz;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.function.common.GetRest;
import com.function.common.RestResponse;
import com.sxbbc.common.basics.exceptions.ThrowJsonException;
import com.sxbbc.common.basics.rest.RestFactory;
import com.sxbbc.common.basics.utils.date.DateUtil;
import com.sxbbc.common.core.entity.jz.ActivitySeckill;
import com.sxbbc.common.core.entity.jz.ActivitySeckillDetail;
import com.sxbbc.common.core.service.*;
import com.sxbbc.common.core.service.system.redis.RedisSeckillService;
import com.sxbbc.common.core.utils.BasePage;
import com.sxbbc.plat.common.ActivityMapping;
import com.sxbbc.plat.util.SystemLog;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;


/**
 * 活动/活动相关 控制层
 *
 * @author leehao
 * @date 2019-06-04 09:52
 */
@SuppressWarnings("all")
@Controller
public class ActivityController {

    @Autowired
    private IProcMainService procMainService;
    @Autowired
    private IProSkuDetailService proSkuDetailService;
    @Autowired
    private IActivityRedPacketService activityRedPacketService;
    @Autowired
    private IActivitySeckillService activitySeckillService;
    @Autowired
    private IActivitySeckillDetailService activitySeckillDetailService;
    @Autowired
    private RedisSeckillService redisSeckillService;


    /**
     * 秒杀活动管理
     * 视图渲染
     * @return org.springframework.web.servlet.ModelAndView
     * @author leehao
     */
    @RequestMapping(value = ActivityMapping.SHOW_SECKILL_LIST)
    public ModelAndView showSeckillList() {
        ModelAndView view = new ModelAndView(ActivityMapping.SHOW_SECKILL_LIST);
        return view;
    }

    /**
     * 秒杀活动管理
     * 数据接口
     * @param page 分页
     * @author leehao
     */
    @RequestMapping(value = ActivityMapping.QUERY_SECKILL_LIST)
    @ResponseBody
    public com.sxbbc.common.basics.rest.RestResponse querySeckillList(BasePage<ActivitySeckill> page){
        QueryWrapper<ActivitySeckill> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("begin_time");
        activitySeckillService.page(page,wrapper);
        return RestFactory.success(page.getRecords(),page.getTotal());
    }

    /**
     * 秒杀信息新增页面
     * @param id 秒杀信息ID
     * @author leehao
     * @return org.springframework.web.servlet.ModelAndView
     */
    @RequestMapping(value = ActivityMapping.SHOW_ADD_SECKILL)
    @ResponseBody
    public ModelAndView showAddSeckill(Integer id) {
        ActivitySeckill activitySeckill = activitySeckillService.getById(id);
        if (activitySeckill != null) {
            DateUtil dateUtil = DateUtil.getInstance();
            activitySeckill.setStartTime(dateUtil.toDateString(activitySeckill.getBeginTime(),dateUtil.DEFAULT_DATETIME_PATTERN));
            activitySeckill.setEndTime(dateUtil.toDateString(activitySeckill.getOverTime(),dateUtil.DEFAULT_DATETIME_PATTERN));
        }
        ModelAndView view = new ModelAndView(ActivityMapping.SHOW_ADD_SECKILL);
        view.addObject("sec", activitySeckill);
        return view;
    }

    /**
     * 新增秒杀信息
     * @param seckill
     * @author leehao
     * @return com.function.common.RestResponse
     */
    @RequestMapping(value = ActivityMapping.ADD_SECKILL)
    @ResponseBody
    @SystemLog(methods = "保存秒杀活动", module = "活动管理")
    public RestResponse addSeckill(ActivitySeckill seckill) {
        if (StringUtils.isBlank(seckill.getName())) {
            return GetRest.getFail("请输入活动名称");
        }
        if (StringUtils.isBlank(seckill.getStartTime())) {
            return GetRest.getFail("请选择活动开始时间");
        }
        if (StringUtils.isBlank(seckill.getEndTime())) {
            return GetRest.getFail("请选择活动结束时间");
        }
        DateUtil dateUtil = DateUtil.getInstance();
        //服务器时间验证
        //服务器时间
        long nowTimes = dateUtil.currentTimeMillis();
        //用户选择的时间
        long userTimes = 0;
        try {
            userTimes = dateUtil.toDateTime(seckill.getStartTime()).getTime();
        } catch (Exception e) {
            e.printStackTrace();
        }
        //相隔时间数
        long between = (nowTimes - userTimes) / (1000*60);
        if ((nowTimes < userTimes || between >= 5) && between > 0) {
            return GetRest.getFail("获取本地时间异常，请勿随意改动本机时间");
        }
        //时间段重复验证
        boolean exist = activitySeckillService.queryNowTimeExist(seckill.getStartTime());
        if (exist) {
            return GetRest.getFail("该时间已存在秒杀活动！");
        }
        seckill.setBeginTime(dateUtil.toDate(seckill.getStartTime(), dateUtil.DEFAULT_DATETIME_PATTERN));
        seckill.setOverTime(dateUtil.toDate(seckill.getEndTime(), dateUtil.DEFAULT_DATETIME_PATTERN));
        boolean save = activitySeckillService.save(seckill);
        if (!save) {
            throw new ThrowJsonException("保存秒杀活动信息失败");
        }
        //设置到Redis
        redisSeckillService.setWillBegin(seckill);
        return GetRest.getSuccess("保存成功");
    }

    /**
     * 处理秒杀信息的状态
     * @param seckill
     * @author leehao
     * @return boolean
     */
    @RequestMapping(value = ActivityMapping.HANDLE_SECKILL_STATUS)
    @ResponseBody
    public RestResponse handleSeckillStatus(ActivitySeckill seckill){
        if (seckill.getId() == null) {
            return GetRest.getFail("获取秒杀活动信息失败");
        }
        if (seckill.getStatus() == null) {
            return GetRest.getFail("获取秒杀活动状态失败");
        }
        return activitySeckillService.handleSeckillStatus(seckill);
    }

    /**
     * 秒杀活动列表
     * 页面
     * @author leehao
     */
    @RequestMapping(value = ActivityMapping.SHOW_SECKILL_DETAIL_LIST)
    public ModelAndView showSeckillDetailList(Integer id) {
        ModelAndView view = new ModelAndView(ActivityMapping.SHOW_SECKILL_DETAIL_LIST);
        view.addObject("seckillId",id);
        return view;
    }

    /**
     * 秒杀活动列表
     * 数据接口
     * @param page 分页
     * @param id 活动表ID
     * @author leehao
     * @return com.sxbbc.common.basics.rest.RestResponse
     */
    @RequestMapping(value = ActivityMapping.QUERY_SECKILL_DETAIL)
    @ResponseBody
    public com.sxbbc.common.basics.rest.RestResponse querySeckillDetailList(BasePage<ActivitySeckillDetail> page, Integer id){
        ActivitySeckillDetail seckillDetail = new ActivitySeckillDetail();
        seckillDetail.setSeckillId(id);
        activitySeckillDetailService.queryList(page, seckillDetail);
        return RestFactory.success(page.getRecords(),page.getTotal());
    }
}
